
// 调入模块
var config = require('./config.json');
var logger = require('./log4js.js');
var client = require('./client.js');
var redis = require('./redis.js');
var md5=require("md5");



//dateFile
/*测试代码 */
/*var r_data = {
	  "notify_url": "http://sup.com/test1",
	  "appkey": "377911bca538467fa257304d974ffd31",
	  "out_order_no": "order_no_xoxoxo_1",
	  "discount_price": "200.000",
	  "result_code": 200,
	  "money": "150.00",
	  "order_no": "201712181550496fb01f2989",
	  "goods_no": "xxxx",
	  "timestamp": 1513583562,
	  "sign": "2623fc74656152a5356a04058b52742a",
	  "message" : "1232"
	};
for(var i =0;i<100;i++){
	redis.lpush( config.SELLER_KEY,JSON.stringify(r_data),function(err,res){
	
		if(err){
			console.log('插入失败');
		}
	});

}*/



/*-----------end-----------*/



logger.info("start_seller");


var arr = [
	{key:config.SELLER_KEY,wait:10},
	{key:config.SELLER_KEY+'_2',wait:5*1000},
	{key:config.SELLER_KEY+'_3',wait:1*60*1000},
	{key:config.SELLER_KEY+'_4',wait:5*60*1000},
	{key:config.SELLER_KEY+'_5',wait:20*60*1000},
	{key:config.SELLER_KEY+'_6',wait:60*60*1000},
];

for(var i=0;i<arr.length;i++){
	redis.lpopRedis(i,arr[i]['key'],arr[i]['wait'],function(index,r_data){
		sendSeller(r_data,index);
	});
}

//发送数据
function sendSeller(r_data,index)
{
	
	if(typeof r_data != "object" ){
		logger.warn('数据不是一个对象');
		return;
	}
	//调用商家链接
	var url = r_data.notify_url;
	var params = {
	  
	    "out_order_no": r_data.out_order_no,
	    "discount_price":r_data.discount_price ,
	    "result_code": r_data.result_code ,
	    "money": r_data.money,
	    "order_no": r_data.order_no,
	    "goods_no": r_data.goods_no,
	    "timestamp": r_data.timestamp,
	    "sign": r_data.sign,
	    "message" :r_data.message
   };
   
	
	client.send(url,params,'post',function(resData){
		if(resData =='success'){
			//商家返回成功
			sendServer(r_data,1);
		}else{
			lpush(r_data,index);
		}
	},function(err){
		lpush(r_data,index);
		logger.warn('server error:'+JSON.stringify(r_data));
	});
}

//放入队列处理
function lpush(r_data,index)
{
	if(index+1 < arr.length){
		logger.info('调取商家失败,加入队列:'+arr[index+1]['key']);
		//插入到下一个队列
		r_data.time = Date.parse(new Date());
		redis.lpush(arr[index+1]['key'],JSON.stringify(r_data),function(err,res){
			if(err){
				logger.warn('redis错误，重新放入：'+ JSON.stringify(r_data));
				//
				redis.lpush(arr[index+1]['key'],JSON.stringify(r_data),function(err,res){
					if(err){
						logger.error('redis错误,不再执行插入：'+JSON.stringify(r_data));
						sendServer(r_data,3);
					}
				});
			}
		});
	}else{
		logger.info('调取商家失败达到最大值,进行回调平台');
		sendServer(r_data,2);
	}
}
//1,成功 2，失败  3，异常
function sendServer(r_data,status)
{

	var url = config.RE_SELLER_URL;
	var method = "put";	
     //参数
    var params = {
		status:status,
		order_no :r_data.order_no,
		timestamp :  Date.parse(new Date())
	};
	params.sign = md5(params.order_no+params.status+params.timestamp+config.secret_key);

	//发送数据
	client.send(url,params,method,function(resData){
   		resData = JSON.parse(resData);
   		if(typeof resData != 'object'){
   			logger.warn('数据不是一个对象');
			return;
   		}
   		if(resData.code==200){
   			logger.info('status:'+status+',反馈成功:'+r_data.order_no);
   		}

	},function(err){
		logger.warn('server error:'+JSON.stringify(r_data));
	});


}

	

	
	










